package com.joyveb.webtest.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;

import com.joyveb.lbos.restful.common.WebRestException;

@Controller
public class RequestExceptionController {
	

	@ResponseBody
	@RequestMapping(value = "/404", method = { RequestMethod.GET, RequestMethod.DELETE, RequestMethod.POST, RequestMethod.PUT })
	@ResponseStatus(value = HttpStatus.NOT_FOUND)
	public String nofund(HttpServletRequest request,HttpServletResponse response) {
		System.out.println(response.getStatus());
		return "404";
	}

	@ResponseBody
	@RequestMapping(value = "/400", method = { RequestMethod.OPTIONS,RequestMethod.HEAD,RequestMethod.GET, RequestMethod.TRACE, RequestMethod.DELETE, RequestMethod.POST, RequestMethod.PUT })
	@ResponseStatus(value = HttpStatus.BAD_REQUEST)
	public String requestParamError() {
		return "400";
	}

	@ExceptionHandler(WebRestException.class)
	@ResponseBody
	@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
	public String handleCustomException(WebRestException ex) {
		ex.printStackTrace();
		return "handleCustomException success";
	}

	@ExceptionHandler(Exception.class)
	@ResponseBody
	@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
	public String handleAllException(Exception ex) {
		ex.printStackTrace();
		return "handleAllException success" + ex.getMessage();
	}

	@ExceptionHandler(MissingServletRequestParameterException.class)
	@ResponseBody
	@ResponseStatus(value = HttpStatus.BAD_REQUEST)
	public String handleMissingServletRequestParameterException(Exception ex) {
		ex.printStackTrace();
		return "MissingServletRequestParameterException success" + ex.getMessage();
	}

	@ExceptionHandler(NoSuchRequestHandlingMethodException.class)
	@ResponseBody
	@ResponseStatus(value = HttpStatus.NOT_FOUND)
	public String handleNoSuchRequestHandlingMethodException(Exception ex) {
		ex.printStackTrace();
		return "NoSuchRequestHandlingMethodException success" + ex.getMessage();
	}
}
